package com.chen.recruit.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chen.recruit.common.annotation.Log;
import com.chen.recruit.common.api.CommonResult;
import com.chen.recruit.common.enums.BusinessType;
import com.chen.recruit.common.valid.AddGroup;
import com.chen.recruit.common.valid.UpdateGroup;
import com.chen.recruit.domain.Notice;
import com.chen.recruit.domain.model.PageQuery;
import com.chen.recruit.service.NoticeService;
import lombok.RequiredArgsConstructor;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;

@Api(tags = "通知公告管理 NoticeController")
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/notice")
public class NoticeController {

    private final NoticeService noticeService;

    @ApiOperation("新增通知公告")
    @SaCheckPermission("system:notice:add")
    @Log(module = "系统管理-通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public CommonResult add(@Validated(AddGroup.class) @RequestBody Notice notice) {
        return noticeService.addOrUpdateNotice(notice);
    }

    @ApiOperation("删除通知公告")
    @SaCheckPermission("system:notice:delete")
    @Log(module = "系统管理-通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public CommonResult delete(@PathVariable Integer[] ids) {
        return noticeService.removeByIds(Arrays.asList(ids)) ? CommonResult.success() : CommonResult.success();
    }

    @ApiOperation("更新通知公告")
    @SaCheckPermission("system:notice:update")
    @Log(module = "系统管理-通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public CommonResult update(@Validated(UpdateGroup.class) @RequestBody Notice notice) {
        return noticeService.addOrUpdateNotice(notice);
    }

    @ApiOperation("获取通知公告详细信息")
    @SaCheckPermission("system:notice:update")
    @GetMapping("/{id}")
    public CommonResult getInfo(@PathVariable Integer id) {
        return CommonResult.success(noticeService.getById(id));
    }

    @ApiOperation("查询通知公告列表")
    @SaCheckPermission("system:notice:list")
    @GetMapping("/list")
    public CommonResult list(Notice notice, PageQuery pageQuery) {
        return CommonResult.success(noticeService.page(pageQuery.build(), buildQueryWrapper(notice)));
    }

    private LambdaQueryWrapper<Notice> buildQueryWrapper(Notice notice) {
        return Wrappers.<Notice>lambdaQuery()
                .eq(ObjectUtil.isNotNull(notice.getType()), Notice::getType, notice.getType())
                .like(StringUtils.isNotBlank(notice.getTitle()), Notice::getTitle, notice.getTitle())
                .like(StringUtils.isNotBlank(notice.getCreateBy()), Notice::getCreateBy, notice.getCreateBy());

    }

}
